package ifips.s7.modele;

import java.util.ArrayList;
import java.util.Iterator;


public class Automate {
    protected String lettre_vide;

    //Listes automates non deterministe
    ArrayList<Etat> Etats;

    public Automate(String vide){
        this.lettre_vide = vide;
        Etats = new ArrayList<Etat>();
    }

    public void addEtat( Etat E){
        this.Etats.add(E);
    }

    public Etat getEtat(int id){

        for (Iterator it = Etats.iterator(); it.hasNext();){

		// itération de la liste

	      	Etat E = (Etat) it.next();

	      	if(E.getId()==id) return E;
		}
    	return null;

    }

    public void addTransition(Etat Dep,String l, Etat Arr ){

        Transition T = new Transition(Dep,l,Arr);
        Dep.addTransition(T);

    }
	

    public boolean isReadable(String mot){
        System.out.println();
        System.out.println("Test de lecture du mot "+mot);
        for (Etat i : Etats){
            if (i.Initial) {
               if (lastEtat(i,mot)) return true;
                
            }
        }
        return false;
    }
     public boolean lastEtat(Etat E, String mot){
    	 if(mot.length()==0) return E.isFinal();
         for (Transition j : E.getTransitions()){
                if (j.lettre.equals(String.valueOf(mot.charAt(0)))){
                    System.out.println("Reste du mot à lire : "+mot+" \t \t Etat courant : "+E.getId());
                    if(lastEtat(j.getArrivee(),mot.substring(1,mot.length()))) return true;
                   
                }
                else if(j.lettre.equals(lettre_vide)){
                    if(lastEtat(j.getArrivee(),mot)) return true;
                }
         }
         System.out.println("Impasse à l'état "+E.getId());
         return false;
     }

     public boolean isFinal(Etat E){

         if(E.isFinal())return true;

         for (Transition j : E.getTransitions()){
                if(j.lettre.equals(lettre_vide)){
                    return isFinal(j.getArrivee());
                }
         }
         return false;

    }
	

	
	boolean mot_reconnu(char mot){
		
		/*Etat_DC E_actu = Etat_Deterministe_Complet.get(0);
		
		for(int i = 0; i < mot.length; i++)
		
		*/
		return true;


	}

        public void print(){

 		for (Iterator it = Etats.iterator(); it.hasNext();){

		// itération de la liste

			Etat E = (Etat) it.next();

			System.out.println("Etat: "+E.getId());
                        if(E.isInitial()) System.out.println("\t Etat Initial");
                        if(E.isFinal()) System.out.println("\t Etat Terminal");
                        System.out.println("\t Transitions: ");

                             for (Iterator it2 = E.getTransitions().iterator(); it2.hasNext();){

                                 Transition T = (Transition) it2.next();

                                 System.out.println("\t lettre: "+T.getLettre()+" vers: "+T.getArrivee().getId());
                    }	

                        
		}

        }

}
